package rsj.service.report;

import com.alibaba.fastjson.JSON;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rsj.api.report.entity.CustomerProductReport;
import rsj.api.report.entity.ProductReport;
import rsj.api.report.entity.ProductReportDetail;
import rsj.api.report.entity.ReportParam;
import rsj.sql.operlog.OperateLogDao;
import rsj.sql.product.ProductReportDao;

import java.util.List;

/**
 * 接口报表
 *
 * @author yuanqinglong
 * @since 2021/7/22 9:12
 */
@Service
public class ProductReportServiceImpl implements IReportService<ProductReport, ProductReportDetail, ReportParam> {



    @Autowired
    private ProductReportDao productReportDao;


    /**
     * 报告列表
     *
     * @param reportParam 报告参数
     * @return {@link List<CustomerProductReport>}
     */
    @Override
    public List<ProductReport> reportList(ReportParam reportParam) {
        TableNode tableNode = productReportDao.productReportList(reportParam);
        List<ProductReport> customerReports = JSON.parseArray(tableNode.toJson(), ProductReport.class);
        customerReports.forEach(productReport -> productReport.processingAttributeValue(reportParam));
        return customerReports;
    }

    /**
     * 报告数
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int reportCount(ReportParam reportParam) {
        return productReportDao.productReportCount(reportParam).valueInt();
    }

    /**
     * 详细的列表
     *
     * @param reportParam 报告参数
     * @return {@link List<ProductReport>}
     */
    @Override
    public List<ProductReportDetail> detailList(ReportParam reportParam) {
        TableNode tableNode = productReportDao.productReportDetailList(reportParam);
        List<ProductReportDetail> productReportDetails = JSON.parseArray(tableNode.toJson(), ProductReportDetail.class);
        productReportDetails.forEach(ProductReportDetail::calculateRate);
        return productReportDetails;
    }

    /**
     * 详细的计算
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int detailCount(ReportParam reportParam) {
        return productReportDao.productReportDetailCount(reportParam).valueInt();
    }



}
